/* Content Manager */

function persistPeople ( jourmois, people ) 
{
	console.log('*** trying to persist : ' + people.nom + ' ***');

	var birthday = JSON.parse( localStorage.getItem( jourmois ) );
	
	if (birthday == null || birthday === null)
	{
		birthday = [];
		birthday[ 0 ] = people;
		console.log( people.nom + " persisted in " + jourmois );
	}
	else
	{
		var exists = false;
	
		for ( var i=0, l=birthday.length; i<l && !exists; i++)
		{
			var someoneElse = birthday[i];
			console.log( 'comparing ' + someoneElse.nom + ' with ' + people.nom );
			
			var someoneMail = new RegExp( someoneElse.mail );
			var someoneFacebook = new RegExp( someoneElse.facebook );
			var someonePlus = new RegExp( someoneElse.plus );
			
			/*if ( people.mail.match( someoneMail ) || 
				 people.facebook.match( someoneFacebook ) || 
				 people.plus.match( someonePlus ))
			{
				console.log( people.mail + " / " + people.facebook +  " / "  + people.plus + " already exists " + someoneElse.mail +  " / "  + someoneElse.facebook +  " / "  + someoneElse.plus );
				exists = true;
			}*/
			
			if (( people.mail == someoneElse.mail ) || 
				( people.facebook == someoneElse.facebook ) || 
				( people.plus == someoneElse.plus ))
			{
				console.log( people.mail + " / " + people.facebook +  " / "  + people.plus + " already exists " + someoneElse.mail +  " / "  + someoneElse.facebook +  " / "  + someoneElse.plus );
				exists = true;
			}
		}
		
		if ( !exists )
		{
			birthday[ birthday.length ] = people;
			console.log( people.nom + " persisted in " + jourmois );
		}
	}

	localStorage.setItem( jourmois, JSON.stringify( birthday ) );
	
	console.log('*** end : ' + people.name + ' ***');
}

function showPeople ()
{
	for ( var i=0, l=localStorage.length; i<l; i++ )
	{
		var key = localStorage.key( i );
		console.log( 'birthday : ' + key );
		var birthday = JSON.parse( localStorage.getItem( key ) );
		
		for ( var j=0, le=birthday.length; j<le; j++)
		{
			var people = birthday[j];
			console.log( 'name : ' + people.nom );
		}	
	}
}

function peopleFromBirthday ( birthday )
{
	console.log(birthday);
    console.log( JSON.parse( localStorage.getItem( birthday ) ) );
    return JSON.parse( localStorage.getItem( birthday ) );
}
/* Init */

function initFakeData ()
{
	createPeople("17/12", "17/12/1987","prenom 1","nom 1","test@test.fr","facebook1","plus1");
	createPeople("17/12", "17/12/1987","prenom 2","nom 2","test@test.fr","facebook2","plus2");
    createPeople("17/12", "17/12/1987","prenom 3","nom 3","test3@test.fr","facebook3","plus3");
	createPeople("17/12", "17/12/1987","prenom 4","nom 4","test4@test.fr","facebook4","plus4");
	
	createPeople("18/05", "18/05/1988","prenom 5","nom 5","test5@test.fr","facebook","plus5");
	createPeople("18/05", "18/05/1988","prenom 6","nom 6","test6@test.fr","facebook","plus6");
    createPeople("18/05", "18/05/1988","prenom 7","nom 7","test7@test.fr","facebook7","plus7");
	createPeople("18/05", "18/05/1988","prenom 8","nom 8","test8@test.fr","facebook8","plus8");
	
	createPeople("29/09", "29/09/1989","prenom 9","nom 9","test9@test.fr","facebook9","plus");
	createPeople("29/09", "29/09/1989","prenom 10","nom 10","test10@test.fr","facebook10","plus");
	createPeople("29/09", "29/09/1989","prenom 11","nom 11","test11@test.fr","facebook11","plus11");
	createPeople("29/09", "29/09/1989","prenom 12","nom 12","test12@test.fr","facebook12","plus12");
}

function createPeople ( jourmois, naissance, prenom, nom, mail, facebook, plus) 
{
	var people = { };
	
	people.nom = nom;
	people.prenom = prenom;
	people.mail = mail;
	people.facebook = facebook;
	people.plus = plus;
	people.naissance = naissance;
	
	persistPeople( jourmois, people );
}

function init () 
{
  	initFakeData();
  	showPeople();
    peopleFromBirthday( "29/09" );
}

init();